package com.application.fabric.users.controller;

import com.application.fabric.users.config.Dealmsg;
import com.application.fabric.users.config.HDFS.HadoopTemplate;
import com.application.fabric.users.entity.Returnmsg;
import com.application.fabric.users.service.TradeService;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/trade")
public class Trade {
    //资产的展示部分 showassets mysql 辅助查询 暂时不做分页
    @Autowired
    private Dealmsg dealmsg;
    @Resource
    private Returnmsg returnmsg;
    @Autowired
    private TradeService tradeService;
    @GetMapping("showassets")
    public Map<String,Object> showassets() throws Exception {

        returnmsg.setData( tradeService.showassetservice());
        returnmsg.setCode(200);
        return  dealmsg.successmsg(returnmsg);
    }

//    展示全部img调用
    @Autowired
    private  HadoopTemplate hadoopTemplate;

@GetMapping("/hdfsimg/{imageName}")
public ResponseEntity readImagesFromHdfs(@PathVariable("imageName") String dirPath) throws Exception {
//    用户目录
    String userpath = "/user/appUser/";
    ResponseEntity r = null;

    RemoteIterator<LocatedFileStatus> files = hadoopTemplate.listFiles(new Path(userpath), false);
    while (files.hasNext()) {
        LocatedFileStatus file = files.next();
        System.out.println(file.getPath().getName());
        if (file.isFile() && file.getPath().getName().equals(dirPath)) {
            try (FSDataInputStream inputStream = hadoopTemplate.open(file.getPath())) {
                // 设置响应头
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.IMAGE_PNG);

                byte[] imageBytes = IOUtils.toByteArray(inputStream);

                 r = new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
            }
        }
    }
    return r;
}
    @GetMapping("/hdfsce/{userid}")
    public ResponseEntity readce(@PathVariable("userid") String dirPath) throws Exception {
//    用户目录
        String userpath = "/user/certificate/"+dirPath;
        ResponseEntity r = null;
        System.out.println(dirPath);
        hadoopTemplate.existDir(userpath,true);
        hadoopTemplate.listFiles(new Path(userpath),true);
        RemoteIterator<LocatedFileStatus> files = hadoopTemplate.listFiles(new Path(userpath), true);
        while (files.hasNext()) {
            LocatedFileStatus file = files.next();
            System.out.println(file.getPath().getName());
            if (file.isFile() && file.getPath().getName().equals(dirPath)) {
                try (FSDataInputStream inputStream = hadoopTemplate.open(file.getPath())) {
                    // 设置响应头
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.APPLICATION_PDF);

                    byte[] imageBytes = IOUtils.toByteArray(inputStream);

                    r = new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
                }
            }
        }
        return r;
    }
}
